
var startupPageID=sessionStorage.getItem("startupPageID")
if(startupPageID!="启动页"){
    querystartupPage()
    function querystartupPage() {
        $.ajax({
            type: "POST",
            url: root.appversioncontrol.readAppScreenMsg,
            cache: false,  //禁用缓存
            dataType: "json",
            async:false,//优先运行
            contentType: "application/json",
            data: JSON.stringify({
                "id":startupPageID,
            }),
            success: function (data) {
                if(data.data!=""||data.data!=null){
                    $("#pictureName").val(data.data[0].pictureName)
                    //启动倒计时
                    if(data.data[0].isCountdown==0){
                        //关闭
                        $(".timeopen").css({
                            display: "none"
                        });
                        $(".timeclose").css({
                            display: "inline"
                        });
                        $("#isCountdown").val("0")
                    }else {
                        //开启
                        $(".timeopen").css({
                            display: "inline"
                        });
                        $(".timeclose").css({
                            display: "none"
                        });
                        $("#isCountdown").val("1")
                        $("#Seconds").val(data.data[0].countdownNum)
                    }
                //启动生效时间
                    if(data.data[0].isValidTime==1){
                        //关闭
                        $(".effectopen").css({
                            display: "none"
                        });
                        $(".effectclose").css({
                            display: "inherit"
                        });
                        $("#isValidTime").val("1")
                    }else {
                        //开启
                        $(".effectopen").css({
                            display: "inline-block"
                        });
                        $(".effectclose").css({
                            display: "none"
                        });
                        $("#isValidTime").val("0")
                        $("#logmin").val(data.data[0].startTime)
                         if(data.data[0].endTime=="1000-01-01 00:00:00"){
                                $("#logmax").val("")
                          }else {
                                $("#logmax").val(data.data[0].endTime)
                           }

                    }
                    if(data.data[0].pitureUrl!=null){
                        var pitureUrls= data.data[0].pitureUrl.split('.')

                            if(pitureUrls[4]=="mp4"){
                                $(".Previews2").css({
                                    display: "inline"
                                });
                                $(".Previews").css({
                                    display: "inline"
                                });
                                $("#Preview").css({
                                    display: "none"
                                });
                                $("#pictures").css({
                                    display: "none"
                                });
                                $(".Previews2").html("<video  controls='' style='width: 375px;height: 630px;position: absolute;top: 107px;' autoplay='' name='media'><source src="+data.data[0].pitureUrl+" type='video/mp4'></video>");
                                $(".Previews").html("<video  controls='' style='width: 95px;height: 97px;margin-top: -8px;margin-left: -3px;' autoplay='' name='media'><source src="+data.data[0].pitureUrl+" type='video/mp4'></video>");
                                $(".launch_rgt").attr("style","top: -204px;")
                                $("#pictures").attr("src", data.data[0].pitureUrl)
                            }else {
                                $(".Previews2").css({
                                    display: "none"
                                });
                                $("#Preview").css({
                                    display: "inline"
                                });
                                $(".Previews").css({
                                    display: "none"
                                });
                                $("#pictures").css({
                                    display: "inline"
                                });
                                $(".launch_rgt").removeAttr("style","top: -204px;")
                                $("#Preview").attr("src", data.data[0].pitureUrl)

                                $("#pictures").attr("src", data.data[0].pitureUrl)
                            }

                        $(".fileupone").html("更换图片")
                    }
                }

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        });
    }
}
//启动倒计时
function timeopen() {
     $(".timeopen").css({
        display: "inline"
    });
    $(".timeclose").css({
        display: "none"
    });

    $("#isCountdown").val("1")

}
//关闭倒计时
function timeclose() {
    $(".timeopen").css({
        display: "none"
    });
    $(".timeclose").css({
        display: "inline"
    });
    $("#isCountdown").val("0")
}
//启动生效时间
function effectopen() {
    $(".effectopen").css({
        display: "inline-block"
    });
    $(".effectclose").css({
        display: "none"
    });

    $("#isValidTime").val("0")

}
//关闭生效时间
function effectclose() {
    $(".effectopen").css({
        display: "none"
    });
    $(".effectclose").css({
        display: "inherit"
    });
    $("#isValidTime").val("1")
}
//图片上传验证
function fileUps() {

    $("#fileUptwo").trigger('click');
}
var formData = new FormData();
// 上传图片返现
function putDataToPage(e,can) {
    verificationPicFileType(can)
    var filePaths = can.value;
    var imgtype=filePaths.substring(filePaths.indexOf("."))
    console.log()
    if(imgtype!=".mp4"){
        verificationPicFileSize(can)
    }else {
        getupfilerul(can,1)
    }

    for(var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if(!(/^image\/.*$/i.test(file.type))) {
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function (e) {
            var data = e.target.result;
            //加载图片获取图片真实宽度和高度
            var img = new Image();
            img.src= data;
            img.onload = function(){
                var width = img.width;
                var height = img.height;

                if (Number(width) == Number(1284) && Number(height) == Number(2778)){
                    getupfilerul(can,2)

                }else {
                    $.Huimodalalert('图片要求1284px*2778px，请重新上传',2000);
                    file.value = "";
                    $(".fileUpone").attr('type','text');
                    $(".fileUpone").attr('type','file');
                    return;
                }
            };
        }
    }
    formData.append('file', $(can)[0].files[0]);
}
// 提交上传图片
function getupfilerul(can,type){
    var formData = new FormData();
    formData.append('file',$(can)[0].files[0]);
    $.ajax({
        url: root.inter.imgUpload,
        cache: false,  //禁用缓存
        type: "POST",
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        data: formData,
        success: function (data) {
            if(type==1){
                $(".Previews2").css({
                    display: "inline"
                });
                $(".Previews").css({
                    display: "inline"
                });
                $("#Preview").css({
                    display: "none"
                });
                $("#pictures").css({
                    display: "none"
                });
                $(".Previews2").html("<video  controls='' style='width: 375px;height: 630px;position: absolute;top: 107px;' autoplay='' name='media'><source src="+data[0].fullPath+" type='video/mp4'></video>");
                $(".Previews").html("<video  controls='' style='width: 95px;height: 97px;margin-top: -8px;margin-left: -3px;' autoplay='' name='media'><source src="+data[0].fullPath+" type='video/mp4'></video>");
                $(".launch_rgt").attr("style","top: -204px;")
                $("#pictures").attr("src", data[0].fullPath)
            }else {
                $(".Previews2").css({
                    display: "none"
                });
                $("#Preview").css({
                    display: "inline"
                });
                $(".Previews").css({
                    display: "none"
                });
                $("#pictures").css({
                    display: "inline"
                });
                $(".launch_rgt").removeAttr("style","top: -204px;")
                $("#Preview").attr("src", data[0].fullPath)
                $("#pictures").attr("src", data[0].fullPath)
            }


            $(can).parent().find('.fileupone').val('更换图片')
            $(".fileUpone").attr('type','text');
            $(".fileUpone").attr('type','file');
        }
    })
}
//点击图片上传
function picturesall(can) {
    var clicks=$(can).parent().parent().parent().children().children()[1]
    var imgsall= $(clicks).children()[1]
    $(imgsall).trigger('click');
}

//保存
function preservation() {
    if($("#pictureName").val()==""){
        $.Huimodalalert('请完善必填信息',2000);
        return false;
    }
    if($("#isCountdown").val()==1){
        if($("#Seconds").val()==""){
            $.Huimodalalert('请完善必填信息',2000);
            return false;
        }
        if($("#Seconds").val()>100){
            $.Huimodalalert('时间不可超过100秒',2000);
            return false;
        }
    }
    if($("#isValidTime").val()==0){
        if($("#logmin").val()==""){
            $.Huimodalalert('请完善必填信息',2000);
            return false;
        }
    }
    if($("#pictures").attr("src")=="../../../img/imgs.png"){
        $.Huimodalalert('请完善必填信息',2000);
        return false;
    }
    var  pictureDrags=$("#pictureDrag").find(".guide_imgsrc")
    var endTimes=""
    var startTimes=""
    if($("#isValidTime").val()==1){
        endTimes="1000-01-01 00:00:00"
        startTimes="1000-01-01 00:00:00"
    }else {
        if($("#logmax").val()!=""){
            endTimes=$("#logmax").val()
        }else {
            endTimes="1000-01-01 00:00:00"
        }
        startTimes=$("#logmin").val()
    }
    var appScreenManage=[]
    var appScreenManages={}
    var urls=""
    if(startupPageID!="启动页"){
        urls=root.appversioncontrol.updateScreenMsg
        appScreenManages={
            "id":startupPageID,
            "pictureName":$("#pictureName").val(),//图片名称
            "pictureType":"2",//页面类型 1.引导页2.启动页3.广告页
            "pitureUrl":$("#pictures").attr("src"),//图片url
            "platformType":"",//平台类型 10:签约平台 20:普通平台
            "areaCode":"",//区域code
            "placeType":"",//插入链接类型
            "placeContent":"",//插入链接内容
            "isSalesCustomer":"",//是否是经销商
            "isCountdown":$("#isCountdown").val(),//是否倒计时
            "isValidTime":$("#isValidTime").val(),//是否开启生效时间
            "startTime":startTimes,
            "endTime":endTimes,
            "countdownNum":$("#Seconds").val(),
            "detailContent":"",
            "updateUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,

        }
        appScreenManage.push(appScreenManages)
    }else {
        urls=root.appversioncontrol.addScreenMsg
        appScreenManages={
            "pictureName":$("#pictureName").val(),//图片名称
            "pictureType":"2",//页面类型 1.引导页2.启动页3.广告页
            "pitureUrl":$("#pictures").attr("src"),//图片url
            "platformType":"",//平台类型 10:签约平台 20:普通平台
            "areaCode":"",//区域code
            "placeType":"",//插入链接类型
            "placeContent":"",//插入链接内容
            "isSalesCustomer":"",//是否是经销商
            "isCountdown":$("#isCountdown").val(),//是否倒计时
            "isValidTime":$("#isValidTime").val(),//是否开启生效时间
            "startTime":startTimes,
            "endTime":endTimes,
            "countdownNum":$("#Seconds").val(),
            "detailContent":"",
            "updateUser":JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,

        }
        appScreenManage.push(appScreenManages)
    }
    $.ajax({
        type: "POST",
        url: urls,
        cache: false,  //禁用缓存
        dataType: "json",
        async:false,//优先运行
        contentType: "application/json",
        data: JSON.stringify({
            "appScreenManage":appScreenManage,
        }),
        success: function (data) {
            if(data.errorCode==0){
                layer.msg("保存成功",{icon: 6,time:2000},function () {
                    if(startupPageID!="启动页"){
                        setTimeout(function () {
                            window.parent.queryopenScreen(parent.comPageNum);
                            cancelLayerNoRefreshFn();
                        })
                    }else {
                        setTimeout(function () {
                            window.parent.queryopenScreen(1);
                            cancelLayerNoRefreshFn();
                        })
                    }
                });
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
//取消
function cancelImg() {
    var cu = layer.confirm('<span>确认取消吗？</span>', {title:'提示',btn: ['确认', '取消']}, function(index){
        parent.layer.closeAll();
        layer.close(cu);
    },function(){
        layer.close(cu);

    });

}
//验证只能输入数字，不能输入0
function Verificationall(val) {
    edit_or_not=false
    val.value = val.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
    val.value = val.value.replace(/^\./g,""); //验证第一个字符是数字
    val.value = val.value.replace(/\.{2,}/g,""); //只保留第一个, 清除多余的
    val.value = val.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    val.value=val.value.replace(/^[0]+[0-9]*$/gi,"")
}
// 图片类型验证
function verificationPicFileType(file) {
    var flag=false;
    var fileTypes = [".jpg", ".jpeg",".gif",".png",".mp4"];
    var filePath = file.value;
    //当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
    if(filePath){
        var isNext = false;
        var fileEnd = filePath.substring(filePath.indexOf("."));
        for (var i = 0; i < fileTypes.length; i++) {
            if (fileTypes[i] == fileEnd) {
                isNext = true;
                flag= true;
                break;
            }
        }
        if (!isNext){
            $.Huimodalalert('格式仅支持jpg 、jpeg 、gif 、png、mp4',2000);
            file.value = "";
            flag=false;
        }
    }
    return flag;
}
//图片大小验证
function verificationPicFileSize(file) {
    var flag=false;
    var fileSize = 0;
    var fileMaxSize = 300;
    var filePath = file.value;
    if(filePath){
        fileSize =file.files[0].size;
        var size = fileMaxSize * Number(1024);
        if (fileSize > size) {
            $.Huimodalalert('图片大小超出300KB',2000);
            file.value = "";
            flag=false;
        }else{
            flag=true;
        }
    }
    return flag;
}